package com.sls.web.manager.game.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.Goods;
import com.sls.web.manager.game.pojo.GoodsBrand;
import com.sls.web.manager.game.pojo.GoodsSupply;
import com.sls.web.manager.game.pojo.NutsNet;
import com.sls.web.manager.game.pojo.OhFly;
import com.sls.web.manager.game.pojo.Tencent;
import com.sls.web.manager.game.service.AuditService;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Repository("auditService")
public class AuditServiceImpl extends SuperService implements AuditService {
	private static Logger log = Logger.getLogger(AuditServiceImpl.class);
	private Gson gson;

	public AuditServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public String addBrand(GoodsBrand goodsBrand, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter AuditService addBrand method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_BRAND_ADD);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goodsBrand.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goodsBrand);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String brandInfo = dto.getBody();
			GoodsBrand goodsBrand2 = gson.fromJson(brandInfo, GoodsBrand.class);
			String brandId = goodsBrand2.getGoodsCategoryId();
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave AuditService addBrand method...");
			return brandId;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public String addGoods(Goods goods, UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter AuditService addGoods method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_ADD);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goods.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goods);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			Goods goods2 = gson.fromJson(dto.getBody(), Goods.class);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave AuditService addGoods method...");
			return goods2.getGoodsId();
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public GoodsSupply addGoodsSupply(GoodsSupply goodsSupply,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter AuditService addGoodsSupply method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_SUPPLY_ADD);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goodsSupply.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goodsSupply);
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Leave AuditService addGoodsSupply method...");
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				GoodsSupply sup = gson.fromJson(body, GoodsSupply.class);
				return sup;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<GoodsBrand> queryBrandList(String originalGoodsId,
			String supplyId, UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter AuditService queryBrandList method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO socketDTO = null;
		if (Constants.OH_FLY.equals(supplyId)) {
			head.setServiceCode(Constants.OH_FLY_AUDIT);
			OhFly ohFly = new OhFly();
			ohFly.setGoodsId(originalGoodsId);
			socketDTO = super.send(head, ohFly);
		}
		if (Constants.NUTS_NET.equals(supplyId)) {
			head.setServiceCode(Constants.NUTS_NET_AUDIT);
			NutsNet nutsNet = new NutsNet();
			nutsNet.setSupProductId(originalGoodsId);
			socketDTO = super.send(head, nutsNet);
		}
		if (Constants.TENCENT.equals(supplyId)) {
			head.setServiceCode(Constants.TENCENT_AUDIT);
			Tencent tencent = new Tencent();
			tencent.setGoodsId(originalGoodsId);
			socketDTO = super.send(head, tencent);
		}
		if (socketDTO != null
				&& socketDTO.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						socketDTO.getHead().getResponseCode())) {
			String body = socketDTO.getBody();
			List<GoodsBrand> goodsBrandList = gson.fromJson(body,
					new TypeToken<List<GoodsBrand>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave AuditService queryBrandList method...");
			return goodsBrandList;
		} else {
			throw new ManagerException(socketDTO.getHead().getResponseMessage());
		}
	}

	@Override
	public List<Goods> queryGoodsListByBrandId(String brandId,
			String goodsState, UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter AuditService queryGoodsListByBrandId method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_GOODSLIST_BY_BRANDID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		Goods goods = new Goods();
		goods.setGoodsCategoryId(brandId);
		goods.setGoodsState(goodsState);
		SocketDTO socketDTO = super.send(head, goods);
		if (socketDTO != null
				&& socketDTO.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						socketDTO.getHead().getResponseCode())) {
			String body = socketDTO.getBody();
			List<Goods> goodsList = gson.fromJson(body,
					new TypeToken<List<Goods>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave AuditService queryGoodsListByBrandId method...");
			return goodsList;
		} else {
			throw new ManagerException(socketDTO.getHead().getResponseMessage());
		}
	}

}
